﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Extensil.Common;
using System.Diagnostics;

namespace TaskAutomation.General
{
    public class BatchProcess : TaskBase
    {
        public override void Start(object param)
        {
            int exitCode;
            ProcessStartInfo processInfo;
            Process process;

            string command = (string)param;
            processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
            processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;

            processInfo.RedirectStandardError = true;
            processInfo.RedirectStandardOutput = true;

            process = Process.Start(processInfo);
            process.WaitForExit();

            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            exitCode = process.ExitCode;

            UpdateStatus(98, "Output: " + (String.IsNullOrEmpty(output) ? "(none)" : output));

            if(!String.IsNullOrEmpty(error))
                UpdateStatus(99, "Error: " + error);

            if(exitCode!=0)
                UpdateStatus(99, "Exit Code: " + exitCode.ToString());

            process.Close();
        }
    }
}
